package cn.git.bbs.core.controller;

import cn.git.bbs.core.bean.product.Brand;
import cn.git.bbs.core.bean.product.Color;
import cn.git.bbs.core.bean.product.Product;
import cn.git.bbs.core.service.brand.BrandService;
import cn.git.bbs.core.service.product.ProductService;
import cn.itcast.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/product/list.do")
    public String list(String name, Integer isShow, Integer pageNo, Model model,Long brand_id){

        Pagination pagination = productService.selectPaginationByQuery(name,isShow,pageNo);

        model.addAttribute("name",name);

        model.addAttribute("pagination",pagination);

        if(isShow!=null){
            model.addAttribute("isShow",isShow);
        }else {
            model.addAttribute("isShow",1);
        }
        if(brand_id!=null){
            model.addAttribute("brand_id",brand_id);
        }else{
            model.addAttribute("brand_id","");
        }


        return"product/list";
    }

    @RequestMapping(value = "/product/toEdit.do")
    public String toEdit(Long id,Model model){
        Product product =  productService.selectProductById(id);
        model.addAttribute(product);
        return "product/edit";
    }

    @RequestMapping(value = "/product/edit.do")
    public String edit(Product product){
        productService.updateProductById(product);

        return "redirect:list.do";
    }

    //加载下拉选
    @RequestMapping(value = "/product/load.do")
    public String loadSelected(HttpServletResponse response) throws IOException {

        return null;
    }
    @Autowired
    BrandService brandService;
    @RequestMapping(value = "/product/toAdd.do")
    public String toAdd(Model model)  {

        List<Brand> brands = new ArrayList<Brand>();
        brands =  brandService.selectBrandList();
        List<Color>  colors = new ArrayList<Color>();
        colors = productService.selectColorList();

        model.addAttribute("brands",brands);
        model.addAttribute("colors",colors);
        return "/product/add";
    }
    @RequestMapping("/product/add.do")
    public String add(Product product) {

        productService.insertProduct(product);

        return "redirect:list.do";
    }
    @RequestMapping("/product/isShow.do")
    public String isShow(Long[] ids){
        productService.isShow(ids);
        return "redirect:list.do";
    }


}